<?php

include 'dbconfig.php'; 
mysql_select_db($db, $con);

$modules = array('','ai','bio','cl','info','math','ninf','phil','psycho');
$credits = array(0,2,4,6,8,12,15,24);
$years = array(2003,2004,2005,2006,2007,2008,2009,2010,2011,2012);

$queries = array(
    'comp' => "SELECT * FROM course_templates WHERE comp = 'comp' ORDER BY name ASC",
    'all' => "SELECT * FROM course_templates ORDER BY comp DESC, name ASC",
    'ex' => "SELECT * FROM course_templates WHERE useradded = 1 ORDER BY name ASC",
    'newex' => "SELECT * FROM course_templates WHERE useradded = 1 AND reviewed = 0 ORDER BY name ASC"
);

if (isset($_POST['query'])) {
    $query = $queries[$_POST['query']];
} else {
    $query = "SELECT * FROM course_templates WHERE comp = 'comp' ORDER BY name ASC";
}

$result = mysql_query($query);

if (mysql_num_rows($result) == 0) {
    echo "<p>No entries found.</p>";
} else {
    
    while ($row = mysql_fetch_array($result)) {
        echo "\t<div class='template' id='template_".$row['id']."'>\n";
        
        // id
        echo "\t\t<span class='id' id='id_".$row['id']."' onclick=\"new Effect.toggle('instances_".$row['id']."', 'blind', {duration: .5});\">".$row['id']."</span>\n";
        // cid
        echo "\t\t<input type='hidden' id='cid_".$row['id']."' value='".$row['cid']."'>\n";
        // name
        echo "\t\t<input type='text' class='name' id='name_".$row['id']."' value='".($row['name'])."'>\n";
        // comp
        echo "\t\t<input type='checkbox' id='comp_".$row['id']."' value='comp' ".($row['comp']=='comp'?"checked":"").">\n";
        // area1
        echo "\t\t<select id='area1_".$row['id']."'>";        
        foreach ($modules as $mod) {
            echo "\t\t\t<option value='$mod' ".($mod==$row['area1']?"selected":"").">$mod</option>\n";
        }
        echo "\t\t</select>\n";
        // area2
        echo "\t\t<select id='area2_".$row['id']."'>\n";        
        foreach ($modules as $mod) {
            echo "\t\t\t<option value='$mod' ".($mod==$row['area2']?"selected":"").">$mod</option>\n";
        }
        echo "\t\t</select>\n";
        // credits
        echo "\t\t<select id='credits_".$row['id']."'>\n";        
        foreach ($credits as $credit) {
            echo "\t\t\t<option value='$credit' ".($credit==$row['ects']?"selected":"").">$credit</option>\n";
        }
        echo "\t\t</select>\n";
        
        echo "\t\t<input type='submit' class='update' onclick=\"updateCourse(".$row['id'].");\" value='Update' />\n";
        echo "\t\t<input type='submit' class='delete' onclick=\"deleteCourse(".$row['id'].");\" value='Delete' />\n";    
        
        echo "\t\t<div class='instances' id='instances_".$row['id']."' style='display:none;'>\n";
        
        // Now to the instances
        $i_query = "SELECT * FROM course_instances WHERE cid = '".$row['cid']."' ORDER BY year ASC, term DESC";
        $i_result = mysql_query($i_query);
        
        while ($i_row = mysql_fetch_array($i_result)) {
    
            echo "\t\t<div class='instance' id='instance_".$i_row['id']."'>\n";
    
            // WS / SS
            echo "\t\t\t<select id='term_".$i_row['id']."'>\n";
            echo "\t\t\t\t<option value='1' ".($i_row['term']==1?"selected":"").">WS</option>\n";
            echo "\t\t\t\t<option value='0' ".($i_row['term']==0?"selected":"").">SS</option>\n";
            echo "\t\t\t</select>\n";
            // year
            echo "\t\t\t<select id='year_".$i_row['id']."'>\n";        
            foreach ($years as $year) {
                echo "\t\t\t\t<option value='$year' ".($year==$i_row['year']?"selected":"").">$year</option>\n";
            }
            echo "\t\t\t</select>\n";
    
            // key
            echo "\t\t<input class='teacher' type='text' id='teacher_".$i_row['id']."' value='".$i_row['teacher']."'>\n";
            echo "\t\t<input class='key' type='text' id='key_".$i_row['id']."' value='".$i_row['coursekey']."'>\n";
        
            echo "\t\t<input type='submit' class='update' onclick=\"updateInstance(".$i_row['id'].",".$row['id'].");\" value='Update' />\n";
            echo "\t\t<input type='submit' class='delete' onclick=\"deleteInstance(".$i_row['id'].",".$row['id'].");\" value='Delete' />\n";    
    
    
    
            echo "\t\t</div>\n";        
        }
        
        
        echo "\t\t<br id='add_".$row['id']."'/><input type='submit' class='add' onclick=\"addInstance('".$row['id']."');\" value='Add Instance' />\n";
        
        echo "\t\t</div>\n\t</div>\n";
    
    }
    
}




mysql_close($con);

?>
